﻿using System;
using System.Collections.Generic;
using System.Text;
using VideoCache.Aggregation.Interfaces;
using Fsb.Utility;

namespace VideoCache.Aggregation.Implements
{
    [Site("letv.com")]
    [Site("letvcdn.com")]
    class LetvFetcher
    {
        public string GetToken(string url)
        {
            string token = this.GetTokenByUrl(url);

            if (string.IsNullOrEmpty(token))
            {
                return this.GetTokenByPlayer(url);
            }

            return token;
        }

        private string GetTokenByUrl(string url)
        {
            string html = HttpUtility.Get(url, Encoding.UTF8);

            return StringUtility.SubString(html, "vid:", ",").Trim();
        }

        private string GetTokenByPlayer(string url)
        {
            // http://player.hz.letv.com/hzplayer.swf/v_list=0?id=1962444&typeFrom=letv_music&callbackJs=LETV.Plugin.Video.refreshState&isPlayerAd=0&pccsUrl=http://www.letv.com/zt/cmsapi/playerapi/pccs_zt_20121130.xml

            string token = UrlHelper.GetParameter(url, "id");

            if (string.IsNullOrEmpty(token))
            {
                token = UrlHelper.GetParameter(url, "vid");
            }

            return token;
        }
    }
}